package com.itheima.mm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mm.common.BaseContext;
import com.itheima.mm.dto.SimpleDto;
import com.itheima.mm.mapper.UserMapper;
import com.itheima.mm.pojo.PageResult;
import com.itheima.mm.pojo.User;
import com.itheima.mm.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * heimamm
 *
 * @ClassName UserServiceImpl
 * @Description
 * @Author 吴志强 --游侠波克比
 * @Date 2022/1/9 21:01
 * @Version V1.0
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    private UserMapper userMapper;

    /**
     * 条件分页查询
     * @param page
     * @param pageSize
     * @param username
     * @return
     */
    @Override
    public PageResult findPage(Integer page, Integer pageSize, String username) {
        //分页过滤
        Page<User> userPage = new Page<>(page, pageSize);
        //非必要条件过滤,name模糊查询
        LambdaQueryWrapper<User> userLqw = new LambdaQueryWrapper<>();
        userLqw.like(username != null,User::getUsername,username);

        //获取结果
        Page<User> selectPage = userMapper.selectPage(userPage, userLqw);
        //封装pageResult
        return new PageResult(selectPage.getTotal(),selectPage.getPages(),selectPage.getPages(),selectPage.getCurrent(),selectPage.getRecords());

    }

    /**
     * 用户状态更改
     * @param id
     * @param disabled
     * @return
     */
    @Override
    @Transactional
    public Boolean disabledStatus(Integer id, Integer disabled) {
        //改变状态0改为1,1改为0
        int status = disabled == 0 ? 1 : 0;

        //状态设置
        LambdaUpdateWrapper<User> userLuw = new LambdaUpdateWrapper<>();
        userLuw.eq(User::getId,id).set(User::getStatus,status);
        userMapper.update(null,userLuw);

        return true;
    }

    /**
     * 简单分页查询
     * @param keyword
     * @param disabled
     * @return
     */
    @Override
    public List<SimpleDto> simple(String keyword, Integer disabled) {
        //条件过滤
        LambdaQueryWrapper<User> userLqw = new LambdaQueryWrapper<>();
        userLqw.eq(keyword != null,User::getUsername,keyword);
        userLqw.eq(disabled != null,User::getStatus,disabled);

        //获取结果
        List<User> users = userMapper.selectList(userLqw);

        //users转化为simple
        List<SimpleDto> simples = users.stream().map((item) -> {
            SimpleDto simple = new SimpleDto();
            simple.setId(item.getId());
            simple.setTitle(item.getPermissionGroupId());
            return simple;
        }).collect(Collectors.toList());

        return simples;
    }

    /**
     * 根据id修改用户密码
     * @param id
     * @param password
     * @return
     */
    @Override
    public Boolean updataPass(Integer id, String password) {
        //获取登陆者id
        Integer userId = BaseContext.get();
        //设置id,模拟用户登陆
        userId = 2;

        //判断用户id的用户是否存在
        User user = userMapper.selectById(id);
        if(user == null){
            return false;
        }

        //todo 修改密码,根据id
        //密码修改前md5加密
        String pass = DigestUtils.md5DigestAsHex(password.getBytes());
        LambdaUpdateWrapper<User> userLuw = new LambdaUpdateWrapper<>();
        userLuw.eq(User::getId,id);
        userLuw.set(User::getPassword,pass);
        userMapper.update(null,userLuw);

        return true;
    }
}
